/*
--------------------------------------------------------------
 class DataType
--------------------------------------------------------------
 constructor: new DataType(type)
   (string) type - represents the data type.

 returns:
   (function) create(values) - create new type.
      (object) values - object that represents values.

      returns: promise

   (function) read(id) - read all or specific item.
      (string)(optional) id - if provided, specific item
      will be readed.

      returns: promise

    (function) update(id, values) - update an item.
      (string) id - specify which item will be updated.
      (object) values - object that represents the new values.

      returns: promise

    (function) delete(id) - delete an item from the type.
      (string) id - the id of the item.

      returns: promise
--------------------------------------------------------------
*/


var Promise = require('bluebird');
var error = require(_ROOT + '/app/errorLogger');
var dataRequest = require(_ROOT + '/app/data/dataRequest');

module.exports = function(dataType) {
  this.type = dataType;
  var self = this;

  function create(values) {
    var def = Promise.defer();
    dataRequest.create(self.type, values).then(function(res) {
      def.resolve(res);
    }).catch(Error, function(e) {
      def.reject(e);
    });
    return def.promise;
  }

  function read(id) {
    var def = Promise.defer();
    dataRequest.read(self.type, id).then(function(res) {
      def.resolve(res);
    }).catch(Error, function(e) {
      def.reject(e);
    });
    return def.promise;
  }

  function del(id) {
    var def = Promise.defer();
    dataRequest.delete(self.type, id).then(function(res) {
      def.resolve(res);
    }).catch(Error, function(e) {
      def.reject(e);
    });
    return def.promise;
  }

  function update(id, values) {
    var def = Promise.defer();
    dataRequest.update(self.type, id, values).then(function(res) {
      def.resolve(res);
    }).catch(Error, function(e) {
      def.reject(e);
    });
    return def.promise;
  }

  return {
    create: create,
    read: read,
    delete: del,
    update: update
  };
}